package com.lz.service.xjl.impl;

import com.lz.entity.*;
import com.lz.repository.xjl.CarDao;
import com.lz.repository.xjl.EquipmentSheetDao;
import com.lz.service.xjl.EquipmentSheetService;
import com.lz.vo.xjl.ListEquipmentSheetVO;
import com.lz.vo.xjl.ModifyEquipmentSheetVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 徐建林
 * @date 2018/10/21 21:19
 */
@Service
public class EquipmentSheetServiceImpl implements EquipmentSheetService {
    @Autowired
    private EquipmentSheetDao equipmentSheetDao;
    @Autowired
    private CarDao carDao;

    @Override
    public List getEquipmentSheetList(Integer pageNo, Integer pageSize, Map<String, Object> map) {

            List<EquipmentSheet> list = equipmentSheetDao.getEquipmentSheetList(pageNo, pageSize, map);
            List<ListEquipmentSheetVO> listEquipmentSheetVOList=new ArrayList<>();
            if (list!=null&&list.size()>0){
                for (EquipmentSheet e:
                list) {
                    ListEquipmentSheetVO vo= new ListEquipmentSheetVO(e);
                    listEquipmentSheetVOList.add(vo);
                    }
                    }
                    return listEquipmentSheetVOList;

    }

    @Override
    public EquipmentSheet getEquipmentById(Integer equipmentSheetNo) {
        return equipmentSheetDao.getEquipmentById(equipmentSheetNo);
    }

    @Override
    public Integer getRows( Map<String, Object> map) {
        return equipmentSheetDao.getRow(map);
    }


    @Override
    public Integer removeEquipmentByNo(Integer equipmentSheetNo) {
        return equipmentSheetDao.removeEquipmentSheetByEquipmentSheetNo(equipmentSheetNo);
    }

    @Override
    public Integer getMaxPrimaryKey() {
        return equipmentSheetDao.getMaxPrimaryKey();
    }

    @Override
    public Integer saveEquipmentSheet(EquipmentSheet equipmentSheet, Car car, Customer customer, List<TemporaryCarParts> temporaryCarPartsList, Employee employee, UpholsteryType upholsteryType) {
        return equipmentSheetDao.saveEquipmentSheet(equipmentSheet, car, customer, temporaryCarPartsList,employee,upholsteryType);
    }

    @Override
    public void cancelEquipmentSheet(Integer equipmentSheetNO) {
         equipmentSheetDao.deleteByEquipmentSheetNO(equipmentSheetNO);
    }

    @Override
    public void modifyEquipment(ModifyEquipmentSheetVO modifyEquipmentSheetVO) {
        //根据EquipmentSheetNo修改汽车
        carDao.updateCar(modifyEquipmentSheetVO.getEquipmentSheetNo(),modifyEquipmentSheetVO.getSeries(),modifyEquipmentSheetVO.getModel(),modifyEquipmentSheetVO.getColor(),modifyEquipmentSheetVO.getVin(),modifyEquipmentSheetVO.getCarCard());
        //修改安装人员
        carDao.updatePerson(modifyEquipmentSheetVO.getEquipmentSheetNo(),modifyEquipmentSheetVO.getPersonNo());
        //修改客户信息
        carDao.updateCustomer(modifyEquipmentSheetVO.getEquipmentSheetNo(),modifyEquipmentSheetVO.getCustomerName(),modifyEquipmentSheetVO.getCustomerTel());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date startTime = format.parse(modifyEquipmentSheetVO.getStartTime());
            Date endingTime = format.parse(modifyEquipmentSheetVO.getEndingTime());
            carDao.updateEquipmentSheet(modifyEquipmentSheetVO.getEquipmentSheetNo(),modifyEquipmentSheetVO.getComment(),startTime,endingTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void setEquipmentSheetType(Integer equipmentSheetNo) {
         equipmentSheetDao.setEquipmentSheetType(equipmentSheetNo);
    }


}
